//
//  BBContainerNodeView.m
//  xNose
//
//  Created by ben smith on 12/09/08.
//  Copyright 2008 __MyCompanyName__. All rights reserved.
//

#import "BBContainerNodeView.h"
#import "BBNode.h"
#import "BBDefaultController.h"
#import "BBImageButtonFactory.h"

@implementation BBContainerNodeView

@synthesize infoButton;

- (id)initWithFrame:(NSRect)frame {
	self = [super initWithFrame:frame];
	if (self) {
		// add the disclosure button
		//[self setWantsLayer:YES];

		[self addInfoButton];
	}
	return self;
}

-(void)setFrame:(NSRect)frame
{
	[super setFrame:frame];
}


-(void)addInfoButton
{
	float borderHeight = [[NSUserDefaults standardUserDefaults] floatForKey:@"BBBorderWidth"];
	float buttonSize = [[NSUserDefaults standardUserDefaults] floatForKey:@"BBInfoButtonSize"];
	NSRect contentRect = [backgroundRect bounds];
	float yOffset = NSMaxY(contentRect) - (buttonSize) - borderHeight;
	NSRect buttonRect = NSMakeRect(NSMaxX(contentRect) - buttonSize - (borderHeight * 2.0), yOffset, buttonSize, buttonSize);
	
	NSButton * button = [BBImageButtonFactory infoButtonInRect:buttonRect];
	[button setTarget:self];
	[button setAction:@selector(infoClick)];
	[button setAutoresizingMask:NSViewMinXMargin | NSViewMinYMargin];
	[self addSubview:button];
	self.infoButton = button;
}

-(void)infoClick
{
	[[NSNotificationCenter defaultCenter] postNotificationName:@"BBOpenInfoNotification" object:self];
}


-(NSRect)textRect
{
	float borderHeight = [[NSUserDefaults standardUserDefaults] floatForKey:@"BBBorderWidth"];
	float fieldHeight  = [BBDefaultController fontHeight:[self font]];
	float textInset = [[NSUserDefaults standardUserDefaults] floatForKey:@"BBTextInsetWidth"];
	
	NSRect textFrame = NSMakeRect(NSMinX(self.bounds) + (textInset * 2.0), NSMaxY(self.bounds) - fieldHeight - (borderHeight * 2), NSWidth(self.bounds) - (textInset * 4.0), fieldHeight);
	
	return textFrame;	
}

-(void)drawNode
{
	[self setupBackgroundRect];
	[self.backgroundGradient drawInBezierPath:backgroundRect angle:90.0];		
	
	NSRect containRect = [backgroundRect bounds];
	float titleHeight = [[NSUserDefaults standardUserDefaults] floatForKey:@"BBTitleBarHeight"];

	[[NSColor grayColor] set];
	[backgroundRect stroke];
	[NSBezierPath strokeLineFromPoint:NSMakePoint(NSMinX(containRect), NSMaxY(containRect) - titleHeight) toPoint:NSMakePoint(NSMaxX(containRect), NSMaxY(containRect) - titleHeight)];

	[node.title drawInRect:[self textRect] withAttributes:textAttributes];

  //super duper sketchy
	float borderHeight = [[NSUserDefaults standardUserDefaults] floatForKey:@"BBBorderWidth"];
	float buttonSize = [[NSUserDefaults standardUserDefaults] floatForKey:@"BBInfoButtonSize"];
	NSRect contentRect = [backgroundRect bounds];
	float yOffset = NSMaxY(contentRect) - (buttonSize) - borderHeight;
	NSRect buttonRect = NSMakeRect(NSMaxX(contentRect) - buttonSize - (borderHeight * 2.0), yOffset, buttonSize, buttonSize);
	
	[infoButton setFrame:buttonRect];	
}

- (void)setupTextAttributes
{
	//[super setupTextAttributes];
	NSMutableParagraphStyle * style = [[NSMutableParagraphStyle alloc] init];
	[style setParagraphStyle:[NSParagraphStyle defaultParagraphStyle]];
	[style setAlignment:NSCenterTextAlignment];
	
	textAttributes = [[NSDictionary dictionaryWithObjectsAndKeys:[self font],NSFontAttributeName,[self fontColor],NSForegroundColorAttributeName,style,NSParagraphStyleAttributeName,nil] retain];
	[style release];	

//	[titleField setAutoresizingMask:NSViewMinYMargin];
}


- (void)setupBackgroundGradient
{
	// create a basic gradient for the background of the view
	CGFloat red1   =   253.0 / 255.0;
	CGFloat green1 =   253.0 / 255.0;
	CGFloat blue1  =  253.0 / 255.0;
	
	CGFloat red2    =  180.0 / 255.0;
	CGFloat green2  =  180.0 / 255.0;
	CGFloat blue2   =  190.0 / 255.0;
	
	NSColor* gradientTop    = [NSColor colorWithCalibratedRed:red1 green:green1 blue:blue1 alpha:1.0];    
	NSColor* gradientBottom = [NSColor colorWithCalibratedRed:red2 green:green2 blue:blue2 alpha:1.0];
	
	NSGradient* gradient;
	gradient = [[NSGradient alloc] initWithStartingColor:gradientBottom endingColor:gradientTop];
	
	self.backgroundGradient = gradient;
	[gradient release];
}
@end
